/****************************************************************************/
/*  File:       GAETools.java                                               */
/*  Author:     F. Georges - H2O Consulting                                 */
/*  Date:       2011-07-21                                                  */
/*  Tags:                                                                   */
/*      Copyright (c) 2011 Florent Georges (see end of file.)               */
/* ------------------------------------------------------------------------ */


package org.fgeorges.gae.tools.saxon;

import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Properties;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import net.sf.saxon.Configuration;
import net.sf.saxon.expr.XPathContext;
import net.sf.saxon.om.Axis;
import net.sf.saxon.om.NodeInfo;
import net.sf.saxon.query.QueryResult;
import net.sf.saxon.trans.XPathException;
import net.sf.saxon.tree.iter.AxisIterator;

/**
 * TODO: ...
 *
 * @author Florent Georges
 * @date   2011-07-21
 */
public class GAETools
{
    public static boolean save(XPathContext ctxt, String uri, NodeInfo node)
            throws XPathException
    {
        DatastoreSlot slot;
        if ( node == null ) {
            slot = new DatastoreSlot(uri, null);
        }
        else {
            AxisIterator  self    = node.iterateAxis(Axis.SELF);
            Configuration config  = ctxt.getConfiguration();
            StringWriter  buffer  = new StringWriter();
            Properties    options = new Properties();
            QueryResult.serializeSequence(self, config, buffer, options);
            slot = new DatastoreSlot(uri, buffer.toString());
        }
        ensureInitObjectify().put(slot);
        return true;
    }

    public static NodeInfo load(XPathContext ctxt, String uri)
            throws XPathException
    {
        Objectify objectify = ensureInitObjectify();
        DatastoreSlot slot = objectify.find(DatastoreSlot.class, uri);
        if ( slot == null ) {
            return null;
        }
        String node = slot.getNode();
        if ( node == null ) {
            return null;
        }
        Source src = new StreamSource(new StringReader(node));
        return ctxt.getConfiguration().buildDocument(src);
    }

    private static Objectify ensureInitObjectify()
    {
        // I don't know how to catch the error if the class is already
        // registered.  Actually, what we need in the first place, is a way to
        // call initialization code when run in Servlex (or any other
        // environment actually).
        try {
            ObjectifyService.register(DatastoreSlot.class);
        }
        catch ( Exception ex ) {
            // FIXME: Not ignore it...!
            ex.printStackTrace();
        }
        // TODO: There is a ".begin()".  Should there be an equivalent ".end()"?
        return ObjectifyService.begin();
    }
}


/* ------------------------------------------------------------------------ */
/*  DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS COMMENT.               */
/*                                                                          */
/*  The contents of this file are subject to the Mozilla Public License     */
/*  Version 1.0 (the "License"); you may not use this file except in        */
/*  compliance with the License. You may obtain a copy of the License at    */
/*  http://www.mozilla.org/MPL/.                                            */
/*                                                                          */
/*  Software distributed under the License is distributed on an "AS IS"     */
/*  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See    */
/*  the License for the specific language governing rights and limitations  */
/*  under the License.                                                      */
/*                                                                          */
/*  The Original Code is: all this file.                                    */
/*                                                                          */
/*  The Initial Developer of the Original Code is Florent Georges.          */
/*                                                                          */
/*  Contributor(s): none.                                                   */
/* ------------------------------------------------------------------------ */
